package com.css.dzj.dzjportal.modules.todo.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjportal.common.util.CvaConstants;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.modules.todo.entity.PortalTodoEntity;
import com.css.dzj.dzjportal.modules.todo.service.PortalTodoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 待办事项controller
 *
 * @author zhouchanghe
 */
@RequestMapping("/todo")
@RestController
public class PortalTodoController {

    @Autowired
    private PortalTodoService portalTodoService;
    @Autowired
    BaseService baseService;
    /**
     * 查询待办
     *
     * @param
     * @return
     * @author zhouchanghe
     * @description TODO
     * @date 2019/12/22 15:44
     **/

    @GetMapping("/findtodoPage")
    public R gettodoPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                         @RequestParam(name = "title", required = false) String title,
                         @RequestParam(name = "station", required = false) String station,
                         @RequestParam(name = "createPerson", required = false) String createPerson,
                         HttpServletRequest request

    ) {

        String status = CvaConstants.STATUSDB;
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        QueryResults<PortalTodoEntity> page = portalTodoService.getPage(title, status, station, createPerson, pageRequest,request);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", page);
    }

    @GetMapping("/firstPageInfo")
    public R getFirstPageInfo(HttpServletRequest request){
        String todoStatus = CvaConstants.STATUSDB;
        PageRequest todoRequest = PageRequest.of(0, 5);
        QueryResults<PortalTodoEntity> todoInfo = portalTodoService.getPage(null, todoStatus, null, null, todoRequest,request);

        String finishStatus = CvaConstants.STATUSYB;
        PageRequest finishRequest = PageRequest.of(0, 5);
        QueryResults<PortalTodoEntity> finishInfo = portalTodoService.getPage(null, finishStatus, null, null, finishRequest,request);

        JSONObject data = new JSONObject();
        data.put("todo", todoInfo);
        data.put("finish", finishInfo);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", data);
    }

    /**
     * 查询已办
     *
     * @param
     * @return
     * @author zhouchanghe
     * @description TODO
     * @date 2019/12/22 13:43
     **/

    @GetMapping("/findtoworkPage")
    public R gettoworkPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                           @RequestParam(name = "title", required = false) String title,
                           @RequestParam(name = "station", required = false) String station,
                           @RequestParam(name = "createPerson", required = false) String createPerson,
                           HttpServletRequest request
    ) {
        String status = CvaConstants.STATUSYB;
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        QueryResults<PortalTodoEntity> page = portalTodoService.getPage(title, status, station, createPerson, pageRequest,request);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", page);
    }

    @PostMapping("/getMyTodo")
    public R getTodoList() {
        try {
            List<PortalTodoEntity> portalTodoEntityList = portalTodoService.queryTodoByLocation();
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("portalTodoEntityList", portalTodoEntityList);
        } catch (Exception e) {
            baseService.logSuccess(3,"查询失败","2");
            e.printStackTrace();
        }
        return R.ok();
    }

    @PostMapping("/saveTodo")
    public R saveTodoList(@RequestBody PortalTodoEntity portalTodoEntity) {
        try {
            portalTodoService.saveDB(portalTodoEntity);
            baseService.logSuccess(3,"添加成功","2");
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"添加失败","2");
            return R.error(e.getMessage());
        }
        baseService.logSuccess(3,"添加成功","2");
        return R.ok();
    }

}
